#include "cpu/exec.h"
//exec_lui
make_EHelper(lui) {
  rtl_sr(id_dest->reg, &id_src->val, 4);

  print_asm_template2(lui);
}

make_EHelper(auipc) {
  rtl_li(&s0,-4);
  rtl_add(&s1, pc, &s0);// this place need to be sub 4 ? I don't known?????????????????
  rtl_add(&id_dest->val, &id_src->val, &s1);
  rtl_sr(id_dest->reg, &id_dest->val, 4);

  print_asm_template2(auipc);
}
// EXW(sll,4), EXW(slt,4) , EXW(sltu,4), EXW(xor,4), EXW(sr,4), EXW(or,4), EXW(and,4)

make_EHelper(add) {

  if(decinfo.isa.instr.funct7 == 0x20  && decinfo.isa.instr.opcode6_2 == 0x0c){
    // Log("sub instruction");
    rtl_sub(&id_dest->val, &id_src->val, &id_src2->val);
    print_asm_template4(sub);
  }else {
    rtl_add(&id_dest->val, &id_src->val, &id_src2->val);
    print_asm_template4(add);
  }
  rtl_sr(id_dest->reg, &id_dest->val, 4);

}

make_EHelper(sll) {
  rtl_shl(&id_dest->val, &id_src->val, &id_src2->val);
  rtl_sr(id_dest->reg, &id_dest->val, 4);
  print_asm_template4(sll);
}

make_EHelper(slt) {
  rtl_setrelop(RELOP_LT,&id_dest->val, &id_src->val, &id_src2->val);
  rtl_sr(id_dest->reg, &id_dest->val, 4);
  print_asm_template4(slt);
}

make_EHelper(sltu) {
  rtl_setrelop(RELOP_LTU,&id_dest->val, &id_src->val, &id_src2->val);
  rtl_sr(id_dest->reg, &id_dest->val, 4);
  print_asm_template4(sltu);
}

make_EHelper(xor) {
  rtl_xor(&id_dest->val, &id_src->val, &id_src2->val);
  rtl_sr(id_dest->reg, &id_dest->val, 4);
  print_asm_template4(xor);
}

make_EHelper(sr) {

  if(decinfo.isa.instr.funct7 == 0x20){ //SRA
    
    rtl_sar(&id_dest->val, &id_src->val, &id_src2->val);
    print_asm_template4(sra);
  }else {
    rtl_shr(&id_dest->val, &id_src->val, &id_src2->val);
    print_asm_template4(srl);
  }
  rtl_sr(id_dest->reg, &id_dest->val, 4);
}

make_EHelper(or) {
  rtl_or(&id_dest->val, &id_src->val, &id_src2->val);
  rtl_sr(id_dest->reg, &id_dest->val, 4);
  print_asm_template4(or);
}

make_EHelper(and) {
  rtl_and(&id_dest->val, &id_src->val, &id_src2->val);
  rtl_sr(id_dest->reg, &id_dest->val, 4);
  print_asm_template4(and);
}